package praktikumid.w09.p5;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

/**
 * Center panel where we draw shapes.
 * @author Ago
 *
 */
public class DrawPanel extends JPanel {
	/**
	 * Start x-point for the oval. 
	 * -1 states that the oval
	 * should not be drawn.
	 */
	private int ovalX = -1;
	/**
	 * Start y-point for the oval.
	 * -1 states that the oval
	 * should not be drawn.
	 */
	private int ovalY = -1;
	
	/**
	 * Constructor, does initialize the panel.
	 * It only sets the background white.
	 */
	public DrawPanel() {
		setBackground(Color.WHITE);
		setPreferredSize(new Dimension(100, 100));
	}
	
	/**
	 * Toggles the oval.
	 * If the oval is not drawn, draw oval.
	 * If the oval is drawn, erases it.
	 * @param x Start x-point of the oval
	 * @param y Start x-point of the oval
	 */
	public void drawOval(int x, int y) {
		if (ovalX == -1) {
			// if oval does not exist
			ovalX = x;
			ovalY = y;
		} else {
			// if oval already exists
			ovalX = -1;
			ovalY = -1;
		}
		// let's force the panel to draw itself
		repaint();
	}
	
	/**
	 * Draws rectangle.
	 * @param x Start x-point of the rectangle
	 * @param y Start y-point of the rectangle
	 */
	public void drawRect(int x, int y) {
		Graphics g = getGraphics();
		g.drawRect(x, y, 60, 200);
		g.dispose();
	}
	/**
	 * Draws line.
	 * @param x Start x-point of the line
	 * @param y Start y-point of the line
	 */
	public void drawLine(int x, int y) {
		Graphics g = getGraphics();
		g.drawLine(x, y, 0, 0);
		g.dispose();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		// overrides paintComponent.
		
		// Executed every time the panel
		// is drawn.
		
		super.paintComponent(g);
		g.setColor(Color.BLUE);
		g.drawLine(200, 10, 10, 200);
		
		if (ovalX > -1) {
			// only draw oval if it should be drawn.
			// if ovalX == -1, then oval is not drawn
			// and the earlier oval (if existed) is
			// replaced with background.
			g.drawOval(ovalX, ovalY, 100, 50);
		}
	}
}
